<?php
/**
 * wondershare站的情人节活动页面接口文件
 * @auth huangcb
 * */

define('INTERFACE_CODE', md5('ws-qrj-sb'));
//note 接口合法性验证
if( $_GET['code'] != INTERFACE_CODE ) exit('error code!');

define('DB_TABLE_PRE','qrj_');//表前缀
define('IMG_SAVE_PATH','/var/www/website/cms.wondershare.com/httpdocs/image2/'.(isset($_POST['img_dir']) ? $_POST['img_dir'] : 'qrj_img/'));
define('IMG_UPLOAD_PATH',IMG_SAVE_PATH.'upload/');
define('IMG_DEFAULT_PATH',IMG_SAVE_PATH.'upload/');

require_once '../../conf/conf.php';
require_once 'lib/Mysql.class.php';
require_once 'lib/Image.class.php';

$db = new Mysql();


//note ws服务器传递图片过来
function uploadimg( $db ){
	require_once 'lib/Upload.class.php';
	$up = new Upload();
	
	$type = 0;
	_createFolder( IMG_UPLOAD_PATH );
	$img = $up->doUpload( IMG_UPLOAD_PATH, 'imgfile', $type );
	
	if( $type != 0 ){//上传失败
		die( $type );
	}else{
		$local_img = IMG_UPLOAD_PATH.$img;
		echo end( explode('/', $local_img) );
	}

}

//note 用户直接分享照片
function deshare( $db ){

	$p['secend'] = $_POST['secend'];
	$p['ip'] = $_POST['ip'];
	$p['time'] = $_POST['time'];

	_createFolder( IMG_DEFAULT_PATH );
	
	$len = strlen( $p['secend'] );
	$im = new Image();

	@copy( IMG_SAVE_PATH.'qrj.png', IMG_DEFAULT_PATH.'qrj.png' );
	$r = $im->watermark( IMG_DEFAULT_PATH.'qrj.png', '', 4, 'To date, I have been with my valentine for', 12, '#5A4942', '../../christmas/class/arial.ttf', 45, 60 );
	$r = $im->watermark( $r, '', 4, $p['secend'], 20, '#EF386C', '../../christmas/class/arial.ttf', 45, 85 );
	$r = $im->watermark( $r, '', 4, 'seconds.', 12, '#5A4942', '../../christmas/class/arial.ttf', 45+$len*15, 90 );
	
	if( is_int( $r ) ){
		exit('error');
	}else{
		$img = end( explode('/', $r));
		rename( $r, str_replace( $img, 'big_'.$img, $r) );
		$p['photo'] = $img;
		$p['type'] = 1;
		_insert( $db, $p );
		exit( $img );
	}
}

//note 用户上传图片并填写信息
function upshare( $db ){

	$p['person_1'] = $_POST['person_1'];
	$p['person_2'] = $_POST['person_2'];
	$p['dowhat'] = $_POST['dowhat'];
	$p['secend'] = abs( $_POST['secend'] );
	$p['photo'] = $_POST['photo'];
	$p['imgpos'] = $_POST['imgpos'];
	$p['ip'] = $_POST['ip'];
	$p['time'] = $_POST['time'];


	$pos = explode( '_', $p['imgpos']  );
	$len = strlen( $p['secend'] );
	$word1 = $p['person_1'] .' & '.$p['person_2'].' ';
	$word2 = 'have been '.$p['dowhat'].' for';
	
	//如果两名字长度小于20，则将dowhat连在一行显示
	if( strlen( $word1 ) < 20 ){
		$word = $word1 . $word2;
		
		//大图上的水印文字换行
		$s1 = $s2 = '';
		$word_arr = explode( ' ', $word );
		foreach( $word_arr as $v ){
			if( strlen( $s1.$v ) < 40 && empty( $s2) ){
				$s1 .= $v.' ';
			}else{
				$s2 .= $v.' ';
			}
		}
		$line = 1;
	}else{
		$line = 2;
	}


	$im = new Image();
	
	//裁剪图片
	$up_c_img = str_replace($p['photo'], 'cut_'.$p['photo'], IMG_UPLOAD_PATH.$p['photo'] );
	$ret = $im->cut( IMG_UPLOAD_PATH.$p['photo'], $up_c_img, $pos[0], $pos[1], $pos[2], $pos[3] );
	
	if( !$ret ){
		exit('error');
	}
	
	
	//生成小图
	@copy( IMG_SAVE_PATH.'qrj_s_bg.png', IMG_UPLOAD_PATH.'qrj_s_bg.png' );
	$up_s_img = str_replace($p['photo'], 'small_'.$p['photo'], IMG_UPLOAD_PATH.$p['photo'] );
	@copy( $up_c_img, $up_s_img );
	$im->zoom( $up_s_img, 168, 120 );
	$r = $im->watermark( IMG_UPLOAD_PATH.'qrj_s_bg.png', $up_s_img, 1, '', 0, '', '', 0, 0 );
	$r = $im->watermark( $r, '', 7, $p['secend'], 12, '#000000', '../../christmas/class/arial.ttf', 0, -4 );
	@unlink( $up_s_img );
	
	if( is_int( $r ) ){
		exit('error');
	}else{
		rename( $r, $up_s_img );
	}

	//生成大图
	@copy( IMG_SAVE_PATH.'qrj_b_bg.png', IMG_UPLOAD_PATH.'qrj_b_bg.png' );
	$up_b_img = str_replace($p['photo'], 'big_'.$p['photo'], IMG_UPLOAD_PATH.$p['photo'] );
	@copy( $up_c_img, $up_b_img );
	$im->zoom( $up_b_img, 322-12, 230 );//12为预留边距
	$info = $im->getInfo( $up_b_img );
	$r = $im->watermark( IMG_UPLOAD_PATH.'qrj_b_bg.png', $up_b_img, 1, '', 0, '', '', max(6,(322-$info['width'])/2), max(6,(230-$info['height'])/2) );
	
	if( $line == 1 ){
		$r = $im->watermark( $r, '', 7, $s1, 12, '#000000', '../../christmas/class/arial.ttf', 10, -100 );
		if( $s2 ) $r = $im->watermark( $r, '', 7, $s2, 12, '#000000', '../../christmas/class/arial.ttf', 10, -80 );
		$r = $im->watermark( $r, '', 7, $p['secend'], 16, '#EF386C', '../../christmas/class/arial.ttf', 10, ($s2 ? -60 : -70) );
		$r = $im->watermark( $r, '', 7, 'seconds.', 12, '#000000', '../../christmas/class/arial.ttf', 10+($len*13), ($s2 ? -60 : -70) );
	}else{
		$r = $im->watermark( $r, '', 7, $word1, 12, '#000000', '../../christmas/class/arial.ttf', 10, -100 );
		$r = $im->watermark( $r, '', 7, $word2, 12, '#000000', '../../christmas/class/arial.ttf', 10, -80 );
		$r = $im->watermark( $r, '', 7, $p['secend'], 16, '#EF386C', '../../christmas/class/arial.ttf', 10, -60 );
		$r = $im->watermark( $r, '', 7, 'seconds.', 12, '#000000', '../../christmas/class/arial.ttf', 10+($len*13), -60 );
	}
	
	@copy( IMG_SAVE_PATH.'qrj_b_bg-h.png', IMG_UPLOAD_PATH.'qrj_b_bg-h.png' );
	$r = $im->watermark( $r, IMG_UPLOAD_PATH.'qrj_b_bg-h.png', 1, '', 0, '', '', 0, 0 );
	
	@unlink( $up_b_img );
	
	if( is_int( $r ) ){
		exit('error');
	}else{
		rename( $r, $up_b_img );
	}
	
	@unlink( $up_c_img );
	
	//以秒数判断年数
	$y = floor($p['secend'] / (365*24*3600));
	if( $y < 1 ){
		$p['year'] = 1;
	}elseif( $y < 2 ){
		$p['year'] = 2;
	}elseif( $y < 5 ){
		$p['year'] = 5;
	}elseif( $y < 10 ){
		$p['year'] = 10;
	}else{
		$p['year'] = 20;
	}
	
	$p['type'] = 2;
	_insert( $db, $p );
	echo $p['photo'];

}


//note 取得随机图片
function get_random_img( $db ){

	$data1 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 20 ORDER BY rand() LIMIT 1' );
	$data2 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 10 ORDER BY rand() LIMIT 1' );
	$data3 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 5 ORDER BY rand() LIMIT 4' );
	$data4 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 2 ORDER BY rand() LIMIT 1' );
	$data5 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 1 ORDER BY rand() LIMIT 11' );
	
	$s = array();
	foreach( $data1 as $k=>$v ) $s[] = '1-'.$v['photo'];
	foreach( $data2 as $k=>$v ) $s[] = '2-'.$v['photo'];
	foreach( $data3 as $k=>$v ) $s[] = ($k+3).'-'.$v['photo'];
	foreach( $data4 as $k=>$v ) $s[] = '7-'.$v['photo'];
	foreach( $data5 as $k=>$v ) $s[] = ($k+8).'-'.$v['photo'];
	
	echo implode(',',$s);
}

//note 取得最新图片
function get_new_img( $db ){
	$data1 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 20 ORDER BY pid DESC LIMIT 1' );
	$data2 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 10 ORDER BY pid DESC LIMIT 1' );
	$data3 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 5 ORDER BY pid DESC LIMIT 4' );
	$data4 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 2 ORDER BY pid DESC LIMIT 1' );
	$data5 = $db->getAll( 'SELECT photo FROM qrj_photo WHERE type = 2 AND year = 1 ORDER BY pid DESC LIMIT 11' );
	
	$s = array();
	foreach( $data1 as $k=>$v ) $s[] = $v['photo'];
	foreach( $data2 as $k=>$v ) $s[] = $v['photo'];
	foreach( $data3 as $k=>$v ) $s[] = $v['photo'];
	foreach( $data4 as $k=>$v ) $s[] = $v['photo'];
	foreach( $data5 as $k=>$v ) $s[] = $v['photo'];
	
	echo implode(',',$s);
}


//note 取得产品剩余数
function get_product_num(){
	if( file_exists(IMG_SAVE_PATH.'product.txt') ){
		$c = file_get_contents( IMG_SAVE_PATH.'product.txt' );
	}else{
		$arr = array( 'win_1'=>99, 'win_2'=>99, 'mac_1'=>99, 'mac_2'=>99 );
		$c = serialize( $arr );
		file_put_contents( IMG_SAVE_PATH.'product.txt', $c );
	}
	
	echo $c;
}

//note 保用户信息
function save_user_info( $db ){
	$photo = $_POST['photo'];
	$param['username'] = $_POST['username'];
	$param['email'] = $_POST['email'];
	
	echo $db->update('photo', $param, "photo='{$photo}'");
}

//note 设置产品剩余数
function set_product_num(){
	$index = $_POST['index'];
	
	$c = file_get_contents( IMG_SAVE_PATH.'product.txt' );
	$arr = unserialize( $c );
	$arr[$index] = (int)$arr[$index] - 1;
	$c = serialize( $arr );
	file_put_contents( IMG_SAVE_PATH.'product.txt', $c );
}

//note 删除图片数据
function del( $db ){
	$p = $_GET['photo'];
	$r = $db->delete('photo', "photo='{$p}'");
	
	header("Content-type:text/html;charset=utf-8");
	
	if( $r ){
		echo '删除数据库成功！','<br/>';
	}else{
		echo '删除数据库失败！','<br/>';
	}
	
	if( file_exists( IMG_UPLOAD_PATH.$p ) ){
		unlink( IMG_UPLOAD_PATH.$p );
		echo '删除：'.IMG_UPLOAD_PATH.$p,'<br/>';
	}else{
		echo '不存在：'.IMG_UPLOAD_PATH.$p,'<br/>';
	}
	
	if( file_exists( IMG_UPLOAD_PATH.'big_'.$p ) ){
		unlink( IMG_UPLOAD_PATH.'big_'.$p );
		echo '删除：'.IMG_UPLOAD_PATH.'big_'.$p,'<br/>';
	}else{
		echo '不存在：'.IMG_UPLOAD_PATH.'big_'.$p,'<br/>';
	}
	
	if( file_exists( IMG_UPLOAD_PATH.'small_'.$p ) ){
		unlink( IMG_UPLOAD_PATH.'small_'.$p );
		echo '删除：'.IMG_UPLOAD_PATH.'small_'.$p ,'<br/>';
	}else{
		echo '不存在：'.IMG_UPLOAD_PATH.'small_'.$p ,'<br/>';
	}
	
	echo 'ok';
}

//---------------------------------------------------------------//
$m = $_GET['m'];
$m( $db );

//创建多级目录
function _createFolder($path, $mask=0777){
   if (!is_dir( $path )){
	    _createFolder(dirname($path));
	    umask(0);
	    mkdir($path, $mask);
   }
}

//数据入库
function _insert( $db, $arr ){
	$data = $_POST;
	$db->insert('photo', $arr + array('secend'=>$data['secend'], 'ip'=>$data['ip'], 'time'=>$data['time']));
}
